package com.ruoyi.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisUtil;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PriceSetting;
import com.ruoyi.system.service.IPriceSettingService;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;

/**
 * 用户VIP 价格显示
Controller
 * 
 * @author DEREK
 * @date 2023-05-22
 */
@RestController
@RequestMapping("/system/systemSetting")
public class PriceSettingController extends BaseController
{
    @Autowired
    private IPriceSettingService priceSettingService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 查询用户VIP 价格显示列表
     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:list')")
    @GetMapping("/list")
    public TableDataInfo list(PriceSetting priceSetting)
    {
    	if(StringUtils.isNotEmpty(priceSetting.getPriceType())) {
    		if(priceSetting.getPriceType().contains(",")) {
    			String[] split = priceSetting.getPriceType().split(",");
    			String type = "";
    			for (String string : split) {
    				type += "'"+string+"',";
				}
    			priceSetting.setPriceType(type.substring(0,type.length()-1));
    		}else {
    			priceSetting.setPriceType("'"+priceSetting.getPriceType()+"'");
    		}
    	}
    	System.out.println("数据："+priceSetting.getPriceType());
        startPage();
        List<PriceSetting> list = priceSettingService.selectPriceSettingList(priceSetting);
        if("subType".equals(priceSetting.getPriceType())) {
        	list.stream().forEach(set ->{
        		if(StringUtils.isNotEmpty(set.getExtend1())) {
        			PriceSetting one = priceSettingService.getOne(new LambdaQueryWrapper<PriceSetting>().eq(PriceSetting::getId, set.getExtend1()).select(PriceSetting::getPriceName));
        			if(null != one && StringUtils.isNotEmpty(one.getPriceName())) {
        				set.setParentName(one.getPriceName());
        			}
        		}
        	});
        }
        return getDataTable(list);
    }

    /**
     * 导出用户VIP 价格显示列表
     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:export')")
    @Log(title = "用户VIP 价格显示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PriceSetting priceSetting)
    {
        List<PriceSetting> list = priceSettingService.selectPriceSettingList(priceSetting);
        ExcelUtil<PriceSetting> util = new ExcelUtil<PriceSetting>(PriceSetting.class);
        util.exportExcel(response, list, "用户VIP 价格显示数据");
    }

    /**
     * 获取用户VIP 价格显示
		详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(priceSettingService.getById(id));
    }

    /**
     * 新增用户VIP 价格显示

     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:add')")
    @Log(title = "用户VIP 价格显示", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PriceSetting priceSetting)
    {
        return toAjax(priceSettingService.save(priceSetting));
    }

    /**
     * 修改用户VIP 价格显示

     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:edit')")
    @Log(title = "用户VIP 价格显示", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PriceSetting priceSetting)
    {
    	if("解锁微信".equals(priceSetting.getPriceType())) {
    		redisUtil.set(ProjectConstant.platformConstant.UNLOCKWECHATPRICE, priceSetting.getCurrentPrice());
    	}else if("解锁聊天".equals(priceSetting.getPriceType())) {
    		redisUtil.set(ProjectConstant.platformConstant.UNLOCKCHATPRICE, priceSetting.getCurrentPrice());
    	}else if("解锁微信次数".equals(priceSetting.getPriceType())) {
    		redisUtil.set(ProjectConstant.platformConstant.UNLOCKWECHATCOUNT, Integer.valueOf(priceSetting.getPriceName()));
    	}else if("解锁聊天次数".equals(priceSetting.getPriceType())) {
    		redisUtil.set(ProjectConstant.platformConstant.UNLOCKCHATCOUNT, Integer.valueOf(priceSetting.getPriceName()));
    	}else if("非会员聊天条数".equals(priceSetting.getPriceType())) {
    		User user = new User();
    		user.setMessageCount(Integer.valueOf(priceSetting.getPriceName()));
    		UpdateWrapper<User> udpWrapper = new UpdateWrapper<User>();
    		udpWrapper.eq("vip", 0);
    		userMapper.update(user, udpWrapper);
    	}else if("VIP充值".equals(priceSetting.getPriceType())) {
    		User user = new User();
    		user.setMessageCount(Integer.valueOf(priceSetting.getExtend5().trim()));
    		user.setUnlockWechat(Integer.valueOf(priceSetting.getExtend2().trim()));
    		user.setChatCount(Integer.valueOf(priceSetting.getExtend3().trim()));
    		UpdateWrapper<User> udpWrapper = new UpdateWrapper<User>();
    		udpWrapper.eq("vip", 1);
    		udpWrapper.eq("vip_level", priceSetting.getExtend4());
    		userMapper.update(user, udpWrapper);
    	}
        return toAjax(priceSettingService.updateById(priceSetting));
    }

    /**
     * 删除用户VIP 价格显示

     */
    @PreAuthorize("@ss.hasPermi('system:systemSetting:remove')")
    @Log(title = "用户VIP 价格显示", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(priceSettingService.deletePriceSettingByIds(ids));
    }
}
